﻿// (c) Copyright Webber-Cross Software Ltd.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.IO;
using System.Xml.Serialization;
using System.Linq;
//using Microsoft.Phone.Shell;
using System.Xml;
using Windows.Storage;
using System.Threading.Tasks;
using Windows.Storage.Streams;
using System.Diagnostics;

namespace WebberCross.Obelisk
{
    public class PersistenceStorage
    {
        private static Type[] _knownTypes = null;

        public static Type[] KnownTypes
        {
            get { return _knownTypes; }
            set { _knownTypes = value; }
        }

        /// <summary>
        /// Serialises and stores an object
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="overwrite"></param>
        /// <returns></returns>
        public static async Task StoreObjectToIsolated<T>(string key, T o, bool overwrite)
        {
            try
            {
                StorageFolder folder = ApplicationData.Current.LocalFolder;
                StorageFile file = await folder.CreateFileAsync(key, CreationCollisionOption.ReplaceExisting);
                
                using (IRandomAccessStream raStream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    using (Stream outStream = raStream.AsStreamForWrite())
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(T), _knownTypes);
                        serializer.Serialize(outStream, o);
                        outStream.Flush();
                    }
                }

            }
            catch (Exception) { }
        }

        /// <summary>
        /// Deserialises and retrieves
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public async static Task<T> GetObjectFromIsolated<T>(string key)
        {
            T o = default(T);

            StorageFolder folder = ApplicationData.Current.LocalFolder;
            StorageFile file = null;

            try
            {
                file = await folder.GetFileAsync(key);
            }
            catch (FileNotFoundException)
            { }

            if (file != null)
            {
                using (IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read))
                {
                    using (Stream inStream = readStream.AsStreamForRead())
                    {
                        try
                        {
                            XmlSerializer serializer = new XmlSerializer(typeof(T), _knownTypes);
                            o = (T)serializer.Deserialize(inStream);
                        }
                        catch (Exception) { }
                    }
                }
            }

            return o;
        }
    }
}
